/*
 * @(#)ShadowedPanel.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.panels;

import org.eoti.awt.GraphicsUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class ShadowedPanel
extends JPanel
{
	protected int depth = 0;
	protected int alpha = 0;

	public ShadowedPanel()
	{
		super();
		setOpaque(false);
	}

	public void setShadowDepth(int depth)
	{
		this.depth = depth;
	}
	public int getShadowDepth(){return depth;}

	public void setAlphaTransparency(int alpha)
	{
		if(alpha < 0) alpha = 0;
		if(alpha > 255) alpha = 255;

		this.alpha = alpha;
	}
	public int getAlphaTransparency(){return alpha;}

	public boolean hasShadow(){return ( (alpha>0) && (depth>0) );}

	public void paint(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		Color cachedColor = g2.getColor();
//		Shape clip = g2.getClip();
		/**
		 * what do we need to do to only draw within the clipped area?
		 */
//		if(!getBounds().intersects(clip.getBounds())) return;

		int width = getBounds().width - depth;
		if(width <= 0) return;

		int height = getBounds().height - depth;
		if(height <= 0) return;

		BufferedImage image = GraphicsUtil.createImage(width, height);
		super.paint(image.getGraphics());

		g2.drawImage(image, depth, depth, this);
		g2.setColor(transparent((getParent() == null ? getBackground() : getParent().getBackground() ), 255-alpha));
		g2.fillRect(depth, depth, width, height);
		g2.drawImage(image, 0, 0, this);
		g2.setClip(0, 0, width, height);
		g2.setColor(cachedColor);
	}

	public Color getBackground()
	{
		return super.getBackground();
	}

	public Color getForeground()
	{
		return super.getForeground();
	}

	protected Color transparent(Color source)
	{
		return transparent(source, alpha);
	}

	protected Color transparent(Color source, int alpha)
	{
		return new Color(source.getRed(), source.getGreen(), source.getBlue(), alpha);
	}
}
